//
//  MILSelectedWiFiViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/19.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILSelectedWiFiViewController.h"
//#import "MILContectWiFiViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>

@interface MILSelectedWiFiViewController ()

@property (weak, nonatomic) IBOutlet UITextField *myWiFiTextField;
@property (weak, nonatomic) IBOutlet UITextField *myWiFiPassWordTextField;
@property (weak, nonatomic) IBOutlet UIButton *hiddenPasswordBtn;
@property (weak, nonatomic) IBOutlet UIImageView *schedule;
@property (weak, nonatomic) IBOutlet UIButton *connectButton;

@end

@implementation MILSelectedWiFiViewController

#pragma mark -- UI
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //请求 wifi名称
    [self fetchNetInfoWiFIName];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"我的设备"];
    self.navigationBar.backgroundColor = [UIColor blackColor];
    [self setLeftButtonImage:[UIImage imageNamed:@"back_icon"]];
    [self hideNavigationBottomLine];
    
}

#pragma mark - the example of how to use executeForResult
- (NSDictionary *)fetchNetInfoWiFIName
{
    NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
    NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
    
    NSDictionary *SSIDInfo;
    for (NSString *interfaceName in interfaceNames) {
        SSIDInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
        
        BOOL isNotEmpty = (SSIDInfo.count > 0);
        if (isNotEmpty) {
            break;
        }
    }
    NSString *ssid = [SSIDInfo objectForKey:@"SSID"] ;
    
    if(ssid)
    {
        NSString *ssids=[NSString stringWithFormat:@"%@",ssid];
        self.myWiFiTextField.text = ssids;
        if (EXRACT_OBJECT(ssid)!=nil) {
            self.myWiFiPassWordTextField.text = EXRACT_OBJECT(ssid);
        }else{
            self.myWiFiPassWordTextField.text = @"";
        }
        
    }
    return SSIDInfo;
}

#pragma mark -- 按钮点击事件
//下一步
- (IBAction)nextStepButtonClickAction:(UIButton *)sender
{
    if (!(self.myWiFiTextField.text.length != 0 && self.myWiFiPassWordTextField.text.length != 0)) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的Wi-Fi密码"];
        return;
    }
    
    [self.myWiFiTextField resignFirstResponder];
    
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 100;
    [self.schedule.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    self.connectButton.userInteractionEnabled = NO;
//    [SVProgressHUD showWithStatus:@"联网中" maskType:SVProgressHUDMaskTypeBlack];
    [[DTCloudManager defaultJNI_iOS_SDK]startDeviceMatchingNetwork:HFLinkMan deviceProductId:TYPEID       deviceName:@"WIFI智能灯" wifiSSID:self.myWiFiTextField.text wifiPassword:self.myWiFiPassWordTextField.text successCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD dismiss];
        DTDevice *device = dic[@"data"];
        if ([device isKindOfClass:[DTDevice class]]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        SAVE_OBJECT(self.myWiFiPassWordTextField.text, self.myWiFiTextField.text);
        SAVE_OBJECT(self.myWiFiTextField.text,SSID);
        NSLog(@"联网SSID：%@",EXRACT_OBJECT(SSID));
        [self.schedule.layer removeAllAnimations];
        self.connectButton.userInteractionEnabled = YES;
    } errorCallback:^(NSDictionary *dic) {
        [SVProgressHUD dismiss];
        NSLog(@"联网：%@",dic);
        [SVProgressHUD showInfoWithStatus:dic[@"errmsg"] maskType:SVProgressHUDMaskTypeBlack];
        [self.schedule.layer removeAllAnimations];
        self.connectButton.userInteractionEnabled = YES;
    }];
    
//    MILContectWiFiViewController *milContectWiFiVC = [[MILContectWiFiViewController alloc]init];
//    milContectWiFiVC.myWiFiNameString = self.myWiFiTextField.text;
//    milContectWiFiVC.myWiFiPasswordString = self.myWiFiPassWordTextField.text;
//    [self.navigationController pushViewController:milContectWiFiVC animated:YES];
}

//隐藏密码
- (IBAction)hiddenMyPasswordClickAction:(UIButton *)sender
{
    self.hiddenPasswordBtn.selected = !sender.selected;
    self.myWiFiPassWordTextField.secureTextEntry = sender.selected;

}

@end
